1207B - Square Filling - CodeForces Solution


constructive algorithms greedy implementation *1200

Please click on ads to support us..

Python Code:

from sys import stdin
input = stdin.readline

inp = lambda : list(map(int,input().split()))

def check(x , y):

    if(x + 1 >= n or y + 1 >= m):return False

    count = a[x][y] + a[x + 1][y] + a[x][y + 1] + a[x + 1][y + 1]
    if(count == 4):return True

    return False

def answer():

    ans = []
    b = [[0 for i in range(m)] for j in range(n)]
    for i in range(n):
        for j in range(m):

            if(check(i , j)):
                b[i][j] = 1
                b[i + 1][j] = 1
                b[i][j + 1] = 1
                b[i + 1][j + 1] = 1
                ans.append([i + 1 , j + 1])

            if(b[i][j] == 0 and a[i][j] == 1):
                if(not check(i , j)):
                    print(-1)
                    return 

    print(len(ans))
    for x in ans:
        print(*x)
    

for T in range(1):

    n , m = inp()
    a = [inp() for i in range(n)]

    answer()

C++ Code:

#include <bits/stdc++.h>
using namespace std;
vector<pair<int , int>> operations;
void check(vector<vector<int>> &v , int x , int y)
{
    int total = v[x][y] + v[x + 1][y] + v[x][y + 1] + v[x + 1][y + 1];
    if(total == 4) operations.push_back({x , y});
}


void testcase()
{
    int n , m; cin >> n >> m;
    vector<vector<int>> v(n , vector<int>(m , 0));
    for(auto &vec : v) for(auto &i : vec) cin >> i;

    for (int i = 0; i < n - 1; ++i)
    {
        for (int j = 0; j < m - 1 ; ++j)
        {
            check(v , i , j);
        }
    }
    

    vector<vector<int>> dup(n , vector<int>(m , 0));

    for(auto &[x , y] : operations)
    {
        int a = x , b = y;

        dup[x][y] = 1;
        dup[x + 1][y] = 1;
        dup[x][y + 1] = 1;
        dup[x + 1][y + 1] = 1;
    }

    if(dup == v)
    {
        cout << operations.size() << endl;
        for(auto &[x , y] : operations)
        {
            cout << x + 1 << " " << y + 1 << endl;
        }
    }
    else cout << -1 << endl;
}

int main() 
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    
    clock_t z = clock();

    int tc = 1;
    // cin >> tc;
    while (tc--) testcase();

    cerr << "Run Time : " << ((double)(clock() - z) / CLOCKS_PER_SEC) << endl;
    return 0;
}


Comments

Submit
0 Comments
More Questions

406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path